const knex = require("../../db/knex");
// const {log} = require("debug");

async function publishQuestionnaire(params, res) {
    console.log(params, '发布信息')
    try{
        await knex.transaction(async trx => {
            await trx('published_questionnaire').insert(params)
            await trx('save_questionnaire').update({is_published:1}).where({id:params.questionnaire_id})
            res.status(200).send({
                msg:"发布成功"
            })
        })
    }catch (e) {
        console.log(e)
    }


}

async function selectPublishedList(res) {
    try {
        await knex.transaction(async trx => {
            let response = await trx('published_questionnaire').select('*')
            console.log(response,'查找结果')
            res.status(200).send({
                response
            })
        })
    } catch (e) {
        console.log(e)
    }
}
async function deletePublished(params, res) {
    let {questionnaire_id}=  params
    console.log(params,'删除id')
    try{
        await knex.transaction(async trx => {
            await trx('published_questionnaire').del().where({questionnaire_id})
          await  trx('save_questionnaire').update({is_published: 0}).where({id: questionnaire_id})
            res.status(200).send({
                msg: "删除成功"
            })
        })
    }catch (e) {
        console.log(e)
    }

}
async function modifyDeadLine(params, res) {
    let {questionnaire_id, deadline} = params
    console.log(params, '修改截止时间')
    try {
        await knex.transaction(async trx => {
            await trx('published_questionnaire').update({deadline}).where({questionnaire_id})
        })
        res.status(200).send({
            msg:"修改成功"
        })
    }catch (e) {
        console.log(e)
    }
}

module.exports = {publishQuestionnaire,selectPublishedList,deletePublished,modifyDeadLine}
